Dropdown List Handling

Multiple Dropdown Fields that Dynamically Update Each Other

Description
This customization shows how to implement multiple Dropdown list fields that dynamically update each other.
Variables
First Dropdown List Control
Select a dropdown list control whose items are populated from a number or character type field in the database
Second Dropdown List Control
Select another dropdown list control whose items are populated from a number or character type field in the database
Parent Record Control
Select the parent record control where the dropdown lists exist.
Table
Select the database table associated with the Parent Record Control
First Dropdown Field
Select the field associated with first dropdown list
Second Dropdown Field
Select the field associated with second dropdown list
Applies to
RecordControl class
Code
 
/// 
/// This method sets the AutoPostBack property of the field that triggers a change.
/// 
/// The object that raised the init event.
/// The object that contains the event data of the init event.
private void MultipleDropdown_MyInit(object sender, System.EventArgs e) 
{   
	// AutoPostBack sets or retrieves a value that indicates whether or not the control
	// posts back to the server each time a user interacts with the control. 
	// if change in second drop down list updates the first then set
	// AutoPostback of the second dropdown list to true and implement 
	${First Dropdown List Control}.AutoPostBack = true;
	${Second Dropdown List Control}.AutoPostBack = true;
    

    // Define selected index changed event handlers
    // for first Dropdown list.
    this.${First Dropdown List Control}.SelectedIndexChanged += 
    new EventHandler(${First Dropdown List Control}_SelectedIndexChanged);
    
    ${Second Dropdown List Control}.Enabled = false;
}
 
Applies to
RecordControl class
Code
 
/// 
/// This method is called when selected index changes in ${First Dropdown List Control}.
/// 
protected override void ${First Dropdown List Control}_SelectedIndexChanged(object sender, System.EventArgs e) 
{ 
    ${Second Dropdown List Control}.Enabled = true;   

    // ${Second Dropdown List Control} DropDownList will display 100 items.
    // You can set the number of items displayed in the DropDownList.
    this.Populate${Second Dropdown List Control}DropDown(100);    
} 
     
Applies to
RecordControl class
Code
 
/// 
/// Override this method to filter the  ${Second Dropdown List Control}DropDownList
/// based on the value selected for the ${First Dropdown List Control}DropDownList
/// 
protected void Populate${Second Dropdown List Control}DropDown(int maxItems)
{
    // Set up the WHERE clause.
    // Create the WHERE clause to filter the second dropdown list based on the 
    // selected value in the first dropdown list.
    WhereClause wc = new WhereClause();
    string selectedValue = ${First Dropdown List Control}.SelectedValue;  
    string selectedText = ${First Dropdown List Control}.SelectedItem.Text;   
    wc.iAND(${${Table}ClassName}.${First Dropdown Field}, BaseFilter.ComparisonOperator.EqualsTo, selectedValue);

    // Clear the contents of second dropdown list.
    this.${Second Dropdown List Control}.Items.Clear();    
    
    // Add "Please Select" string to second dropdown list.   
    this.${Second Dropdown List Control}.Items.Insert(0, new ListItem(Page.GetResourceValue("Txt:PleaseSelect", "${Application Name}"), "--PLEASE_SELECT--"));                      
    
    if(BaseClasses.Utils.StringUtils.InvariantUCase(selectedText).Equals(BaseClasses.Utils.StringUtils.InvariantUCase(Page.GetResourceValue("Txt:PleaseSelect", "${Application Name}"))))
    {
        // if "Please Select" string is selected for first dropdown list,
        // then do not continue populating the second dropdown list.
        return;
    }    
    
    // Get the records using the created where clause.
    foreach ( ${${Table}RecordClassName} itemValue in ${${Table}ClassName}.GetRecords(wc, null, 0, maxItems))
    {
        if(itemValue.${Second Dropdown Field}Specified)
        {
            // In each record, obtain the value of second dropdown field if value exists,
            // create an item for it and add it to the list.
            string cvalue = itemValue.${Second Dropdown Field}.ToString();
            string fvalue  = itemValue.Format(${${Table}ClassName}.${Second Dropdown Field});
            ListItem item  = new ListItem(fvalue, cvalue);
            if (! this.${Second Dropdown List Control}.Items.Contains(item))
            {
                this.${Second Dropdown List Control}.Items.Add(item);        
            }
        }        
    }                    

    // Select "Please Select" string in the second dropdown list.
    this.${Second Dropdown List Control}.SelectedIndex = 0;
}
    
Applies to
CSharpRecordControlConstructor class
Code
 
    // The following line will be inserted inside the
    // constructor for page class.
    this.Init += new EventHandler(MultipleDropdown_MyInit);
     

Terms of Service Privacy Statement